package com.yinhai.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yinhai.asynctask.AsyncTaskParam;
import com.yinhai.asynctask.LoginTask;
import com.yinhai.asynctask.NameValueParam;
import com.yinhai.constant.UrlConstant;

import org.apache.http.NameValuePair;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginForPwdActivity extends Activity {


    @BindView(R.id.edt_phone)
    EditText edtPhone;
    @BindView(R.id.img_delete)
    ImageView imgDelete;
    @BindView(R.id.edt_pwd)
    EditText edtPwd;
    @BindView(R.id.img_delete_pwd)
    ImageView imgDeletePwd;
    @BindView(R.id.btn_login)
    TextView btnLogin;
    @BindView(R.id.txw_login_cloud)
    TextView txwLoginCloud;


    boolean focusPhone;
    boolean focusPwd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_for_pwd);
        ButterKnife.bind(this);


        edtPwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                focusPwd = hasFocus;
                handleEdtPwdView();
            }
        });

        edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                focusPhone = hasFocus;
                handleEdtPhoneView();
            }
        });


        edtPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                handleEdtPwdView();
            }
        });


        edtPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                handleEdtPhoneView();
            }
        });

    }

    private void handleEdtPhoneView() {
        if (!focusPhone || TextUtils.isEmpty(edtPhone.getText().toString())) {
            imgDelete.setVisibility(View.GONE);
        }else {
            imgDelete.setVisibility(View.VISIBLE);
        }
    }

    private void handleEdtPwdView() {
        if (!focusPwd || TextUtils.isEmpty(edtPwd.getText().toString())) {
            imgDeletePwd.setVisibility(View.GONE);
        }else {
            imgDeletePwd.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.img_delete)
    public void onImgDeleteClicked() {
        edtPhone.setText("");
    }

    @OnClick(R.id.img_delete_pwd)
    public void onImgDeletePwdClicked() {
        edtPwd.setText("");
    }

    @OnClick(R.id.btn_login)
    public void onBtnLoginClicked() {


        String phone = edtPhone.getText().toString();
        String pwd = edtPwd.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            return;
        }

        if (TextUtils.isEmpty(pwd)) {
            return;
        }

        LoginTask loginTask = new LoginTask(this);
        AsyncTaskParam taskParam = new AsyncTaskParam(UrlConstant.LOCAL_URI+"appCascheck");
        List<NameValuePair> list = new ArrayList<NameValuePair>();

        NameValueParam user_phone = new NameValueParam();
        user_phone.setName("user_phone");
        user_phone.setValue(phone);
        list.add(user_phone);

        NameValueParam pwdP = new NameValueParam();
        pwdP.setName("pwd");
        pwdP.setValue(pwd);
        list.add(pwdP);

        NameValueParam flag = new NameValueParam();
        flag.setName("flag");
        flag.setValue("0");
        list.add(flag);

        taskParam.setPostParam(list);
        loginTask.execute(taskParam);

    }

    @OnClick(R.id.txw_login_cloud)
    public void onTxwLoginCloudClicked() {
        // TODO: 2018/6/7


    }
}